package com.zwf.controller.user;

import com.zwf.entity.AddressBook;
import com.zwf.result.Result;
import com.zwf.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zwf
 * @date 2024/2/10 19:49
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿接口")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有信息")
    public Result<List<AddressBook>> list(){
        log.info("查询当前登录用户的所有信息");
        List<AddressBook> addressBookList=addressBookService.list();
        return Result.success(addressBookList);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAdd(@RequestBody AddressBook addressBook){
        log.info("设置默认地址：{}",addressBook);
        addressBookService.setDefaultAdd(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> findDefault(){
        log.info("查询默认地址");
        AddressBook addressBook=addressBookService.findDefault();
        if(addressBook.getId()!=null){
            return Result.success(addressBook);
        }
        return Result.error("没有查到默认地址");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址：{}",id);
        AddressBook addressBook=addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id){
        log.info("根据id删除地址：{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result modifyById(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址：{}",addressBook);
        addressBookService.modify(addressBook);
        return Result.success();
    }

}
